<template>
  <div class="book-management">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>图书管理系统</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="showAddDialog">添加新书</el-button>
      </div>
      
      <!-- 图书表格 -->
      <el-table :data="books" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="title" label="书名"></el-table-column>
        <el-table-column prop="author" label="作者"></el-table-column>
        <el-table-column prop="copies" label="库存数量"></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加图书对话框 -->
      <el-dialog title="添加新书" :visible.sync="dialogVisible" width="30%">
        <el-form :model="newBook" :rules="rules" ref="bookForm" label-width="80px">
          <el-form-item label="书名" prop="title">
            <el-input v-model="newBook.title"></el-input>
          </el-form-item>
          <el-form-item label="作者" prop="author">
            <el-input v-model="newBook.author"></el-input>
          </el-form-item>
          <el-form-item label="库存数量" prop="copies">
            <el-input-number v-model="newBook.copies" :min="1"></el-input-number>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAdd">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'BookManagement',
  data() {
    return {
      books: [
        {"id": 1, "title": "Python编程", "author": "John Doe", "copies": 4},
        {"id": 2, "title": "历史的秘密", "author": "Jane Smith", "copies": 3},
        {"id": 3, "title": "宇宙探索", "author": "Emily Brown", "copies": 2},
        {"id": 4, "title": "健康生活", "author": "Mike Johnson", "copies": 5},
        {"id": 5, "title": "数学之美", "author": "Sarah Lee", "copies": 4},
        {"id": 6, "title": "艺术欣赏", "author": "David White", "copies": 3},
        {"id": 7, "title": "编程入门", "author": "Alice Green", "copies": 2},
        {"id": 8, "title": "科幻世界", "author": "Frank Black", "copies": 5},
        {"id": 9, "title": "自然奇观", "author": "Grace Blue", "copies": 4},
        {"id": 10, "title": "音乐之声", "author": "Tom Red", "copies": 3}
      ],
      dialogVisible: false,
      newBook: {
        title: '',
        author: '',
        copies: 1
      },
      rules: {
        title: [
          { required: true, message: '请输入书名', trigger: 'blur' }
        ],
        author: [
          { required: true, message: '请输入作者', trigger: 'blur' }
        ],
        copies: [
          { required: true, message: '请输入库存数量', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    showAddDialog() {
      this.dialogVisible = true
      this.newBook = {
        title: '',
        author: '',
        copies: 1
      }
    },
    handleAdd() {
      this.$refs.bookForm.validate((valid) => {
        if (valid) {
          const newId = this.books.length > 0 ? Math.max(...this.books.map(book => book.id)) + 1 : 1
          this.books.push({
            id: newId,
            ...this.newBook
          })
          this.dialogVisible = false
          this.$message.success('添加成功')
        }
      })
    },
    handleDelete(index, row) {
      this.$confirm('确认删除这本书吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.books.splice(index, 1)
        this.$message.success('删除成功')
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
}
</script>

<style scoped>
.book-management {
  padding: 20px;
}
.box-card {
  width: 100%;
}
</style> 